package greedy.找零钱;

public class leetcode860柠檬水找零 {
    public boolean lemonadeChange(int[] bills) {
        int length = bills.length;
        int money5,money10;
        money5 = money10 = 0;
        for (int i = 0; i < length; i++) {
            if (bills[i] == 5) {
                money5++;
            } else if (bills[i] == 10) {
                if (money5 > 0) {
                    money5--;
                } else {
                    return false;
                }
                money10++;
            } else {
                if (money10 > 0 && money5 > 0) {
                    money10--;
                    money5--;
                } else if (money5 >= 3) {
                    money5 -= 3;
                } else {
                    return false;
                }
            }
        }
        return true;
    }

    // 评论抄袭优化版，无论顾客花费多少钱买柠檬水我们要想顺利找零，手中必须要有5美元的零钱，否则无法完成找零。
    public boolean lemonadeChange1(int[] bills) {
        int length = bills.length;
        int money5,money10;
        money5 = money10 = 0;
        for (int i = 0; i < length; i++) {
            if (bills[i] == 5) money5++;
            else if (bills[i] == 10) { money5--; money10++; }
            else if (money10 > 0) { money10--; money5--; }
            else { money5 -= 3; }
            if (money5 < 0) return false;
        }
        return true;
    }
}
